package solved;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;

/*
 ID:	liluyan1
 LANG:	JAVA
 TASK:	gift1
 */
public class gift1 {
	public static void main(String[] args) throws IOException {
		BufferedReader f = new BufferedReader(new FileReader("gift1.in"));
		Giver[] giver = new gift1().run(f);
		
		PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(
				"gift1.out")));
		for (Giver g : giver) {
			out.println(g.name + " " + g.balance);
		}
		out.close();
		System.exit(0);
	}
	
	private Giver[] run(BufferedReader f) throws IOException {
		Giver[] giver = readData(f);
		compute(giver);
		return giver;
	}
	
	private void compute(Giver[] giver) {
		int N = giver.length;
		HashMap<String, Integer> map = new HashMap<String, Integer>();
		for (int i=0; i<N; i++) {
			map.put(giver[i].name, i);
		}
		
		for (int i=0; i<N; i++) {
			giver[i].balance -= giver[i].amount;
			for (int r : giver[i].receivers) {
				giver[r].balance += giver[i].amount / giver[i].receivers.length;
			}
		}
	}
	
	private Giver[] readData(BufferedReader f) throws IOException {
		int N = Integer.parseInt(f.readLine());
		Giver[] g = new Giver[N];
		HashMap<String, Integer> map = new HashMap<String, Integer>();
		
		for (int i=0; i<N; i++) {
			g[i] = new Giver(f.readLine().trim());
			map.put(g[i].name, i);
		}
		
		for (int i=0; i<N; i++) {
			String name = f.readLine().trim();
			int k = map.get(name);
			
			String[] s = f.readLine().split(" ");
			int amount = Integer.parseInt(s[0]);
			int number = Integer.parseInt(s[1]);
			g[k].receivers = new int[number];
			g[k].amount = number>0? (amount / number) * number : 0; 
			for (int j=0; j<number; j++) {
				g[k].receivers[j] = map.get(f.readLine().trim());
			}
		}
		
		return g;
	}
	
	private class Giver {
		String name;
		int amount;
		int balance = 0;
		int[] receivers;
		
		public Giver(String name) {
			this.name = name;
		}
	}
}
